package com.pospi.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.Log;
import android.widget.Toast;

import com.digi.module.Printer.IPrinter;
import com.digi.port.*;
import com.google.zxing.BarcodeFormat;
import com.king.zxing.util.CodeUtils;
import com.lany.sp.SPHelper;
import com.pospi.dto.GoodsDto;
import com.pospi.pai.yunpos.login.Constant;
import com.pospi.pai.yunpos.more.ScaleGoodsActivity;

/**
 * Created by acer on 2018/11/12.
 */

public class TabPrintHelper {
    private Context context;
    private GoodsDto dto;

    public TabPrintHelper(Context context, GoodsDto dto) {
        this.context = context;
        this.dto = dto;
    }

    public void printTab() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i("tag", dto.getCode());
                String code = getCode(dto);


                Bitmap b = CodeUtils.createBarCode(code, BarcodeFormat.CODE_128, 270, 60,null,true,13, Color.BLACK);
                Bitmap bitmap = StarPrintHelper.creatBq(dto, context, b);
                if (com.digi.port.Printer.printer2GetDeviceEnabled()) {
                    final int status = com.digi.port.Printer.printer2GetStatus();
                    if (status == IPrinter.S_OK) {
                        final boolean resultp = com.digi.port.Printer.printer2PrintBitmap(bitmap);
                        final boolean resultf = com.digi.port.Printer.printer2FeedPaper(0);
                        final boolean resultc = com.digi.port.Printer.printer2CutPaper();
                    } else {
//                        context.runOnUiThread(new Runnable() {
//                            @Override
//                            public void run() {
//                                Toast.makeText(ScaleGoodsActivity.this, "打印异常："+getprinterStatusDescription(status), Toast.LENGTH_SHORT).show();
//                            }
//                        });
                    }
                } else {
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            Toast.makeText(ScaleGoodsActivity.this, "未连接到打印机", Toast.LENGTH_SHORT).show();
//                        }
//                    });
                }


            }
        }).start();
    }
    public void printTabNum() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i("tag", dto.getCode());
                String code = getCode(dto);


                Bitmap b = CodeUtils.createBarCode(code, BarcodeFormat.CODE_128, 270, 60,null,true,13, Color.BLACK);
                Bitmap bitmap = StarPrintHelper.creatBq2(dto, context, b);
                if (com.digi.port.Printer.printer2GetDeviceEnabled()) {
                    final int status = com.digi.port.Printer.printer2GetStatus();
                    if (status == IPrinter.S_OK) {
                        final boolean resultp = com.digi.port.Printer.printer2PrintBitmap(bitmap);
                        final boolean resultf = com.digi.port.Printer.printer2FeedPaper(0);
                        final boolean resultc = com.digi.port.Printer.printer2CutPaper();
                    } else {
//                        context.runOnUiThread(new Runnable() {
//                            @Override
//                            public void run() {
//                                Toast.makeText(ScaleGoodsActivity.this, "打印异常："+getprinterStatusDescription(status), Toast.LENGTH_SHORT).show();
//                            }
//                        });
                    }
                } else {
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            Toast.makeText(ScaleGoodsActivity.this, "未连接到打印机", Toast.LENGTH_SHORT).show();
//                        }
//                    });
                }


            }
        }).start();
    }




    public static String getCode(GoodsDto dto) {
        double amount = DoubleSave.doubleSaveTwo(dto.getNum() * dto.getPrice());
        String m = "";//实际金额
        String flag = SPHelper.getInstance().getString(Constant.POS_DZC_FLAG);//标志位
        int lenth = 13 - flag.length() - 5;//剩余位数
        int len = SPHelper.getInstance().getInt(Constant.POS_DZC_NUM);//保留位数

        int a = 0;
        switch (len) {
            case 0:
                a = (int) (amount*10);
                if (lenth == 6) {
                    m = String.format("%06d", a);
                } else {
                    m = String.format("%07d",a);//其中0表示补零而不是补空格，6表示至少6位
                }

                break;
            case 1:
                a = (int) (amount*100);
                if (lenth == 6) {
                    m = String.format("%06d", a);
                } else {
                    m = String.format("%07d",a);//其中0表示补零而不是补空格，6表示至少6位
                }
                break;
            case 2:
                a = (int) (amount*1000);
                if (lenth == 6) {
                    m = String.format("%06d", a);
                } else {
                    m = String.format("%07d",a);//其中0表示补零而不是补空格，6表示至少6位
                }
                break;
            default:
                a = (int) (amount*10);
                if (lenth == 6) {
                    m = String.format("%06d", a);
                } else {
                    m = String.format("%07d",a);//其中0表示补零而不是补空格，6表示至少6位
                }

                break;
        }

        String code = flag + dto.getPlu() + m;
        Log.i("tag", "条码：" + code);


        return code;
    }
}
